package com.atguigu.tingshu.payment.api;

import com.atguigu.tingshu.common.login.RequiresLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.atguigu.tingshu.vo.payment.vo.WxPrePayParamsVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment")
@Slf4j
public class WxPayApiController {

    private final WxPayService wxPayService;

    public WxPayApiController(WxPayService wxPayService) {
        this.wxPayService = wxPayService;
    }


    @Operation(summary = "微信下单，获取微信小程序拉起本地微信支付所需要参数")
    @Parameters({
            @Parameter(name = "paymentType", description = "支付类型", required = true),
            @Parameter(name = "orderNo", description = "订单号", required = true)
    })
    @RequiresLogin
    @PostMapping("/wxPay/createJsapi/{paymentType}/{orderNo}")
    public Result<WxPrePayParamsVo> getWxPrePayParams(@PathVariable String paymentType, @PathVariable String orderNo) {
        WxPrePayParamsVo wxPrePayParamsVo = wxPayService.getWxPrePayParams(paymentType, orderNo);
        return Result.ok(wxPrePayParamsVo);
    }


    @Operation(summary = "根据商户订单编号查询支付状态")
    @Parameters({
            @Parameter(name = "outTradeNo", description = "商户订单编号", required = true)
    })
    @GetMapping("/wxPay/queryPayStatus/{outTradeNo}")
    public Result<Boolean> queryPayStatus(@PathVariable String outTradeNo) {
        Boolean isPay = wxPayService.queryPayStatus(outTradeNo);
        return Result.ok(isPay);
    }

    /**
     * 微信支付异步回调
     *
     * @param request 微信支付异步回调请求
     * @return 微信支付异步回调响应
     */
    @Operation(summary = "微信支付异步回调")
    @PostMapping("/wxPay/notify")
    public Map<String, String> paySuccessNotify(HttpServletRequest request) {
        return wxPayService.paySuccessNotify(request);
    }
}
